package com.fxd.crm.web.controller.courseOrderBase;

import com.fxd.crm.model.CourseOrder;
import com.fxd.crm.model.PageResult;
import com.fxd.crm.model.curdResult;
import com.fxd.crm.service.ICourseOrderBaseService;
import com.fxd.crm.service.ICourseOrderService;
import com.fxd.crm.service.impl.courseOrderImpl.CourseOrderImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @auther fuxudong
 * @date 2021/4/3 - 14:03
 */
@RestController
@RequestMapping("/courseOrderBase")
public class CourseOrderBodyBaseController {
    @Autowired
    ICourseOrderBaseService courseOrderBaseImpl;

    @Autowired
    ICourseOrderService courseOrderService;

    /**
     * page:显示当前页
     * limit：每次显示多少条
     *
     * @return
     */
    @RequestMapping("listjson")
    public PageResult<CourseOrder> listJson( CourseOrder courseOrder,int page, int limit) {
        PageResult<CourseOrder> pageResult = courseOrderBaseImpl.findPageResult(courseOrder, page, limit);
        return pageResult;
    }

    @RequestMapping("stuorderlist")
    public PageResult<CourseOrder>studentorderlist(HttpServletRequest request,int page, int limit) {
        String name = (String)request.getSession().getAttribute("name");
        PageResult<CourseOrder> pageResult = courseOrderBaseImpl.findPageResultByName(name, page, limit);
        return pageResult;
    }

    @RequestMapping("save")
    public curdResult save(CourseOrder order) {
        curdResult curdResult;
        if (order.getOrder_id() != null) {
            curdResult = new curdResult(1, "成功");
            courseOrderBaseImpl.update(order);
        } else {
            curdResult = new curdResult(1, "成功");
            courseOrderBaseImpl.save(order);
            System.out.println();
        }
        return curdResult;
    }

    @RequestMapping("put")
    @Transactional
    public curdResult put(Integer order_id){
        CourseOrder courseOrder = courseOrderService.findByOrderId(order_id);
        courseOrderService.deleteByOrderId(order_id);
        courseOrderBaseImpl.save(courseOrder);
        return new curdResult();
    }

    @RequestMapping("delete")
    public curdResult delete(Integer order_id) {
        curdResult curdResult = new curdResult();
        courseOrderBaseImpl.deleteByOrderId(order_id);
        System.out.println(curdResult);
        return curdResult;

    }


}
